package com.kvest.testtask.core;

import java.util.ArrayList;

import android.util.Pair;

import com.kvest.testtask.datamodel.Site;
import com.kvest.testtask.datastorage.SettingsStorage;
import com.kvest.testtask.datastorage.SitesSQLite;
import com.kvest.testtask.network.GetSitesRequest;

public class SitesManager 
{
	private static SitesManager manager;
	private static final Object lock = new Object();
	
	private SitesChangeListener sitesChangeListener;
	
	public static SitesManager getManager()
	{
		synchronized (lock) 
		{
			if (manager == null)
			{
				manager = new SitesManager();
			}
		}
		
		return manager;
	}

	private ArrayList<Site> sites;
	private SitesSQLite sitesDB;
	private SettingsStorage settingsStorage;
	
	private SitesManager()
	{
		sites = new ArrayList<Site>();
		sitesDB = new SitesSQLite(TestTaskApplication.getAppContext());
		settingsStorage = new SettingsStorage(TestTaskApplication.getAppContext());
		sitesChangeListener = null;
		
		//Load sites from local DB
		load();
		
		//Try load sites xml, if it wasn't made
		if (!settingsStorage.isXMLLoaded())
		{
			loadXML();
		}
	}
	
	public void setSitesChangeListener(SitesChangeListener sitesChangeListener)
	{
		this.sitesChangeListener = sitesChangeListener;
	}
	
	public void load()
	{
		//clear all
		sites.clear();
		
		//load from DB
		sitesDB.loadSites(sites);
	}
	
	public void getSitesList(ArrayList<Pair<Integer, String>> items)
	{
		//Clear previouse info
		items.clear();
		
		//Add new info
		for (Site site : sites)
		{
			items.add(new Pair<Integer, String>(site.getFeatureId(), site.getName()));
		}
	}
	
	private void loadXML()
	{
		GetSitesRequest request = new GetSitesRequest(new GetSitesRequest.GetSitesResult() 
		{
			@Override
			public void sitesResult(int resultType, Site[] loadedSites) 
			{
				if (resultType == GetSitesRequest.GetSitesResult.OK_RESULT)
				{
					//save sites in manager
					for (int i = 0 ; i < loadedSites.length; ++i)
					{
						if (loadedSites[i] != null)
						{
							addSite(loadedSites[i]);
						}
					}
					
					//set xml loaded
					settingsStorage.setXMLLoaded(true);
				}
				
			}
		});
		request.sendRequest();
	}
	
	public void addSite(Site site)
	{
		//find if site with the same id exists and delete it
		for (Site siteTmp : sites)
		{
			if (siteTmp.getFeatureId() == site.getFeatureId())
			{
				sites.remove(siteTmp);
				
				//emit delete site event
				emitDeleteSiteEvent(siteTmp.getFeatureId());
				
				break;
			}
		}
		
		//add and save new site
		sites.add(site);
		sitesDB.addSite(site);
		
		//emit new site event
		emitNewSiteEvent(site);
	}
	
	public boolean hasSite(int id)
	{
		for (Site site : sites)
		{
			if (site.getFeatureId() == id)
			{
				return true;
			}
		}
		
		return false;
	}
	
	public Site getSite(int id)
	{
		for (Site site : sites)
		{
			if (site.getFeatureId() == id)
			{
				return site;
			}
		}
		
		return null;
	}
	
	public void updateSite(int id, Site newSite)
	{
		ToDo Kvest
	}
	
	public void deleteSite(int id)
	{
		for (Site siteTmp : sites)
		{
			if (siteTmp.getFeatureId() == id)
			{
				sites.remove(siteTmp);
				
				//delete from DB
				sitesDB.deleteSite(id);
				
				//emit delete site event
				emitDeleteSiteEvent(siteTmp.getFeatureId());
				
				break;
			}
		}
	}
	
	private void emitNewSiteEvent(Site site)
	{
		if (sitesChangeListener != null)
		{
			sitesChangeListener.newSite(site);
		}
	}
	
	private void emitDeleteSiteEvent(int id)
	{
		if (sitesChangeListener != null)
		{
			sitesChangeListener.deleteSite(id);
		}
	}
	
	private void emitUpdateSiteEvent(Site site)
	{
		if (sitesChangeListener != null)
		{
			sitesChangeListener.updateSite(site);
		}
	}
	
	public interface SitesChangeListener
	{
		public void newSite(Site site);
		public void updateSite(Site site);
		public void deleteSite(int id);
	}
}
